<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2016/8/11
 * Time: 14:01
 */
namespace YMApi\Model;
use Think\Model;

class AdModel extends Model
{
    protected $tableName = 'ad';


    /*
     * 根据广告位置ID查询
     * 返回该广告位置下所有广告数据
     * */
    public function get_adlist($page,$position_id){
        $where['position_id']=$position_id;
        $where['start_time']=array('ELT',gmtime());
        $where['end_time']=array('GT',gmtime());
        // $where['_string'] ='start_time<='.gmtime().' and end_time>'.gmtime().'';
        $count=$this->get_adcount($where);
        $pagearr=show_page($count,$page);
        if($page>=$pagearr['pagecount']){
            $is_last=1;
        }else{
            $is_last=0;
        }
        $res=$this->field('ad_id,position_id,media_type,ad_name,ad_link,ad_code,start_time,end_time,link_man,link_email,link_phone,click_count,enabled')
            ->where($where)
            ->limit($pagearr['firstpage'].','.C('page_size'))
            ->order('sort_order desc,ad_id desc')
            ->select();

        //dump($this->getlastsql());
        //dump($res);
        if($res){
            $data=array('list'=>$res,'is_bottom'=>$is_last,'page'=>$pagearr);
            return $data;
        }else{
            return false;
        }
    }
    /*
 * 根据广告位置ID查询
 * 返回该广告位置下所有广告数据
 * */
    public function get_adlist2($page,$position_id){
        $where['position_id']=$position_id;
        $where['start_time']=array('ELT',gmtime());
        $where['end_time']=array('GT',gmtime());
        // $where['_string'] ='start_time<='.gmtime().' and end_time>'.gmtime().'';
        $count=$this->get_adcount($where);
        $pagearr=show_page($count,$page);
        if($page>=$pagearr['pagecount']){
            $is_last=1;
        }else{
            $is_last=0;
        }
        $res=$this->field('ad_id,position_id,media_type,ad_name,ad_link,ad_code,start_time,end_time,link_man,link_email,link_phone,click_count,enabled')
            ->where($where)
            ->limit($pagearr['firstpage'].','.C('page_size'))
            ->order('sort_order desc,ad_id desc')
            ->select();
        //dump($this->getlastsql());
        //dump($res);
        if($res){
            $data=array('list'=>$res,'is_bottom'=>$is_last,'page'=>$pagearr);
            return $data;
        }else{
            return false;
        }
    }
    /*
     * 根据广告位置ID查询 
     * 获取广告总数
     * */
    public function get_adcount($where){
        return $this->where($where)->count();
    }
}